package com.chuangjiangx.member.stored.ddd.domain.model;

import com.chuangjiangx.dddbase.domain.shared.Timestamp;
import com.chuangjiangx.domain.shared.model.PayEntry;
import com.chuangjiangx.domain.shared.model.PayTerminal;
import com.chuangjiangx.domain.shared.model.PayType;
import com.chuangjiangx.member.coupon.ddd.domain.model.MbrHasCouponId;
import com.chuangjiangx.member.basic.ddd.domain.model.MemberId;
import com.chuangjiangx.member.basic.ddd.domain.model.MerchantId;
import lombok.Getter;

import java.math.BigDecimal;
import java.util.Date;

/**
 * @author cj-ch
 * @date 2018/4/4 下午6:30
 */
@Getter
public class MbrOrderMirror {
    private Long id;
    private Date createTime;
    private Date updateTime;
    /**
     * 会员订单编号
     */
    private String orderNumber;
    /**
     * 订单类型
     */
    private Integer type;
    /**
     * 商户
     */
    private Long merchantId;
    /**
     * 商户用户
     */
    private Long merchantUserId;
    /**
     * 门店
     */
    private Long storeId;
    /**
     * 门店用户
     */
    private Long storeUserId;
    /**
     * 会员
     */
    private Long memberId;
    /**
     * 订单金额
     */
    private BigDecimal orderAmount;
    /**
     * 订单优惠金额
     */
    private BigDecimal discountAmount;
    /**
     * 实际支付金额
     */
    private BigDecimal paidAmount;
    /**
     * 退款金额
     */
    private BigDecimal refundAmount;
    /**
     * 支付时间
     */
    private Date paidTime;
    /**
     * 退款时间
     */
    private Date refundTime;
    /**
     * 会员领取的卡券/或充值赠送德卡券
     */
    private Long mbrHasCouponId;
    /**
     * 订单状态
     */
    private MbrOrderStatus status;
    /**
     * 支付入口
     */
    private PayEntry payEntry;
    /**
     * 支付方式
     */
    private PayType payType;

    private String remark;
    /**
     * 支付终端
     */
    private PayTerminal payTerminal;
    /**
     * orderpay表的订单编号
     */
    private String orderPayNumber;
    /**
     * 充值规则Id
     */
    private Long mbrRechargeRuleId;


    public static final MbrOrderMirror of(MbrOrder mbrOrder){
        MbrOrderMirror mirror = new MbrOrderMirror();
        mirror.id = mbrOrder.getId().getId();
        Timestamp timestamp = mbrOrder.getTimestamp();
        if(null != timestamp){
            mirror.createTime = timestamp.getCreateTime();
            mirror.updateTime = timestamp.getUpdateTime();
        }
        mirror.orderNumber = mbrOrder.getOrderNumber();
        mirror.type = mbrOrder.getType().value;
        mirror.merchantId = mbrOrder.getMerchantId().getId();
        mirror.merchantUserId = mbrOrder.getMerchantUserId();
        mirror.storeId = mbrOrder.getStoreId();
        mirror.storeUserId = mbrOrder.getStoreUserId();
        mirror.memberId = mbrOrder.getMemberId().getId();
        mirror.orderAmount = mbrOrder.getOrderAmount();
        mirror.discountAmount = mbrOrder.getDiscountAmount();
        mirror.paidAmount = mbrOrder.getPaidAmount();
        mirror.refundAmount = mbrOrder.getRefundAmount();
        mirror.paidTime = mbrOrder.getPaidTime();
        mirror.refundTime = mbrOrder.getRefundTime();
        MbrHasCouponId mbrHasCouponId = mbrOrder.getMbrHasCouponId();
        if(mbrHasCouponId != null){
            mirror.mbrHasCouponId = mbrHasCouponId.getId();
        }
        mirror.status = mbrOrder.getStatus();
        mirror.payEntry = mbrOrder.getPayEntry();
        mirror.payType = mbrOrder.getPayType();
        mirror.remark = mbrOrder.getRemark();
        mirror.payTerminal = mbrOrder.getPayTerminal();
        mirror.orderPayNumber = mbrOrder.getOrderPayNumber();
        MbrRechargeRuleId mbrRechargeRuleId = mbrOrder.getMbrRechargeRuleId();
        if(mbrRechargeRuleId != null){
            mirror.mbrRechargeRuleId = mbrRechargeRuleId.getId();
        }
        return mirror;
    }

    public MbrOrder convertToMbrOrder(){
        MbrOrder o = new MbrOrder(
                orderNumber,MbrOrderType.of(type),new MerchantId(merchantId),merchantUserId,storeId,
                storeUserId,new MemberId(memberId),orderAmount,discountAmount,
                paidAmount,
                refundAmount,paidTime,refundTime,
                mbrHasCouponId == null ? null : new MbrHasCouponId(mbrHasCouponId),
                status,payEntry,
                payType,remark,payTerminal,orderPayNumber,
                mbrRechargeRuleId == null ? null : new MbrRechargeRuleId(mbrRechargeRuleId)
        );
        o.setId(new MbrOrderId(id));
        o.setTimestamp(new Timestamp(createTime,updateTime));
        return o;
    }

}
